package com.crm.web;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 拦截器
 */
public class AuthorizationInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws IOException {
        if (request.getSession().getAttribute("loginUser") == null) {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.println("<script>alert('请先进行登录，在进行后续操作！(Interceptor控制)');location.href='"+request.getContextPath()+"/login.jsp';</script>");
            return false;
        } else {
            return true;
        }
    }
}
